package com.zidian.teacher.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import com.zidian.teacher.utils.DensityUtils;

/**
 * Created by Administrator on 2016/9/9 0009.
 *
 * 用于画评价反馈的条形图
 *
 */
public class BarView extends View {

    private Context context;
    private Paint mPaint;//画笔
    private int startX = 0;//画布的原点X（所有的画图操作，都是基于这个原点的）
    private int startY = 0;//画布的原点Y（所有的画图操作，都是基于这个原点的）
    private int sideheight = 20;//左边，上面bar的宽度
    private int sidewidth = 30;//左边，上面bar的宽度
    private int eachBoxH = 135;//每个格子的高度，设置可以后面根据屏幕对它做了均分
    private int eachBoxW = 120;//每个格子的宽度，后面根据屏幕对它做了均分
    private int classTotal = 6;//左边栏总格子数
    private int dayTotal = 4;//x方向格子数，后面动态分配

    public static final int barBg = Color.argb(255, 159, 159, 159);
    public static final int markerBorder = Color.argb(255, 232, 232, 232);
    public static final int testred = Color.argb(255, 81, 209, 196);
    public static final int testblue = Color.argb(255, 252, 173, 81);

    public static final String[] YText = {"5", "4", "3", "2", "1", "0"};
    public static final String[] XText = {"学生评价", "督导评价", "同行评价"};


    public BarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        dp2px();
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        eachBoxW = getWidth() / 3;
        eachBoxH = (getHeight() - sideheight) / classTotal;
//        sideheight = eachBoxH / 2;

        printLeftBar(canvas);
        printBottomBar(canvas);
        printPonit(canvas);

    }

    /**
     * 屏幕适配
     */
    private void dp2px(){
        sideheight = DensityUtils.dip2px(context,sideheight);
        sidewidth = DensityUtils.dip2px(context,sidewidth);
        eachBoxH = DensityUtils.dip2px(context,eachBoxH);
        eachBoxW = DensityUtils.dip2px(context,eachBoxW);
    }

    private void printMarker(Canvas canvas) {
        mPaint.setColor(markerBorder);

        for (int j = 0; j < classTotal - 1; j++) {
            mPaint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(
                    sidewidth,
                    (j + 1) * eachBoxH,
                    eachBoxW * dayTotal,
                    (j + 1) * eachBoxH + 1,
                    mPaint);

        }
    }

    private void printLeftBar(Canvas canvas) {
        mPaint.setColor(barBg);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(30);

        Rect textRect1 = new Rect();
        mPaint.getTextBounds("1", 0, 1, textRect1);
        int th = textRect1.bottom - textRect1.top;
        int tw1 = textRect1.right - textRect1.left;
        mPaint.getTextBounds("1.5", 0, 2, textRect1);
        int tw2 = textRect1.right - textRect1.left;

        for (int i = 0; i < classTotal; i++) {
            // 画文字
            int tw = tw1 * 2 + (tw2 - tw1) * (i / 10);
            canvas.drawText(YText[i], sidewidth / 2 - tw / 2, startY
                    + eachBoxH * (i + 1) + th / 2, mPaint);
        }


    }

    private void printBottomBar(Canvas canvas) {
        mPaint.setColor(barBg);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(30);

        Rect textRect1 = new Rect();
        mPaint.getTextBounds("学生评价", 0, 1, textRect1);
        int th = textRect1.bottom - textRect1.top;
        int tw1 = textRect1.right - textRect1.left;

        for (int i = 1; i < dayTotal; i++) {
            // 画文字
            canvas.drawText(XText[i - 1], startX + sidewidth + eachBoxW * (i - 1) + eachBoxW / 4 - tw1 / 2, startY + eachBoxH * classTotal + sideheight / 2 + th / 2, mPaint);
        }
    }


    private void printPonit(Canvas canvas) {
        mPaint.setColor(testred);
        mPaint.setStyle(Paint.Style.FILL);

        Rect textRect1 = new Rect();
        mPaint.getTextBounds("1", 0, 1, textRect1);
        int th = textRect1.bottom - textRect1.top;
        int tw1 = textRect1.right - textRect1.left;

        if (list.size() != 0) {
            for (int i = 1; i < dayTotal; i++) {
                // 画文字
                mPaint.setColor(testred);
                mPaint.setStyle(Paint.Style.FILL);
                int ain = ((int) (list.get(i - 1).getAverage() / 1));
                int as = ((int) (list.get(i - 1).getAverage() * 10)) % 10;
                canvas.drawRect(startX + sidewidth + eachBoxW * (i - 1) + eachBoxW / 4 + tw1 / 2, (eachBoxH * classTotal) - eachBoxH * ain - eachBoxH / 10 * as, startX + sidewidth + eachBoxW * (i - 1) + eachBoxW / 4 + 40, (eachBoxH * classTotal), mPaint);// 长方形
                mPaint.setColor(testblue);
                mPaint.setStyle(Paint.Style.FILL);
                int sin = ((int) (list.get(i - 1).getScored() / 1));
                int ss = ((int) (list.get(i - 1).getScored() * 10)) % 10;
                int d = ((int) (list.get(i - 1).getScored() * 100)) % 10;
                canvas.drawRect(startX + sidewidth + eachBoxW * (i - 1) + eachBoxW / 4 + 40, (eachBoxH * classTotal) - eachBoxH * sin - eachBoxH / 10 * ss - eachBoxH / 100 * d, startX + sidewidth + eachBoxW * (i - 1) + tw1 / 2 + eachBoxW / 4 + 80, (eachBoxH * classTotal), mPaint);// 长方形

            }
        }


    }

    private List<BarBean> list = new ArrayList<>();

    public void setData(List<BarBean> list) {
        this.list = list;
        invalidate();// 刷新页面
    }

}
